package com.it.aop.demoaop01.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Author: xiaoqiang
 * @Description:
 * @Date: 2021/6/10
 * @Version: 1.0
 */
public class ConnectionUtil {

    private static ThreadLocal<Connection> threadLocal = new ThreadLocal();

    public static Connection getConnection() {
        Connection connection = null;
        connection = threadLocal.get();
        if (connection == null) { //创建新链接
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
                threadLocal.set(connection);
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
}
